parser grammar UriParser;

options {
  language = Java;
  tokenVocab = UriLexer;
  output = AST;
}

// List of tokens used to structure the tree
tokens {
  ABS;
  REL;
  URIS;
}

@members {
  public Collection<Problem> problems = new ArrayList<Problem>();

      public void displayRecognitionError(String[] tokenNames,
                                        RecognitionException e) {
          Problem problem = MocaFactory.eINSTANCE.createProblem();
          int line  = e.line;
          int charPos = e.charPositionInLine;
          int tokenLenght = 1;
          Token token = e.token;
          if(token != null) 
            tokenLenght = token.getText().length();
                  
          
          problem.setType(ProblemType.ERROR);
          problem.setLine(line); 
          problem.setCharacterPositionStart(charPos); 
          problem.setCharacterPositionEnd(charPos+tokenLenght);
          problem.setMessage("Parser: " + getErrorMessage(e, tokenNames));
          
          problems.add(problem);
          super.displayRecognitionError(tokenNames, e);
    }
}

@header {
package org.moflon.moca.uri.parser; 
import java.util.Collection;
import Moca.MocaFactory;
import Moca.Problem;
import Moca.ProblemType;
}

// Parser Rules:
main: uri+ 
	-> ^(URIS uri+);

uri : absolute -> absolute | relative 
	-> relative;

absolute : scheme=STRING COLON schemeSpecPart=STRING 
	-> ^(ABS $scheme $schemeSpecPart);

relative : STRING -> ^(REL STRING);